$(function () {
	let myCharts = echarts.init(document.getElementById('cityMap'));
	//存储切换的每一级地图信息
	var mapStack = [];
	var timeFn = null;
	var curMap = {};
	var lastPoints = [];
	var currentMapcode = '';
	var series = [];
	var tf=[];
	var tv=[];
	var te=[];
	var tfault=[];
	/**
   加载地图：根据地图所在省市的行政编号，
   获取对应的json地图数据，然后向echarts注册该区域的地图
   最后加载地图信息
   @params {String} mapCode:地图行政编号,for example['中国':'100000', '湖南': '430000']
   @params {String} mapName: 地图名称
*/
	var convertData = function (data) { // 处理数据函数
		var res = [];
		for (var i = 0; i < data.length; i++) {
			var geoCoord = data[i].coord;
			if (geoCoord) {
				res.push({
					name: data[i].name,
					value: geoCoord.concat(data[i].value),
					type: data[i].type,
					flag: data[i].flag
				});
			}
		}
		return res;
	};
	var fconvertData = function (data) {
		var res = [];
		for (var i = 0; i < data.length; i++) {
			var dataItem = data[i];
			var fromCoord = geoCoordMap[dataItem[0].name];
			var toCoord = geoCoordMap[dataItem[1].name];
			if (fromCoord && toCoord) {
				res.push({
					fromName: dataItem[0].name,
					toName: dataItem[1].name,
					coords: [fromCoord, toCoord],
					value: dataItem[1].value
				});
			}
		}
		return res;
	};
	loadMap('trainmap', '科佳')
	/**
   绑定双击事件，并加载上一级地图
*/
	myCharts.on('dblclick', function (params) {
		//当双击事件发生时，清除单击事件，仅响应双击事件
		clearTimeout(timeFn);
		var map = mapStack.pop();
		if (!mapStack.length && !map) {
			// alert('已经到达最上一级地图了');
			return;
		}
		loadMap(map.mapCode, map.mapName);
	});
	myCharts.on('click', function (params) {
		clearTimeout(timeFn);
		//由于单击事件和双击事件冲突，故单击的响应事件延迟250毫秒执行
		timeFn = setTimeout(function () {
			if (params.data && params.data.flag) {
				window.location.href = './assets/' + params.data.name + '.xlsx'
			} else {
				var name = params.name;
				var mapCode = cityMap[name];
				if (!mapCode) {
					// alert('暂无' + name + '更详细的地图!');
					return;
				}
				if (currentMapcode != mapCode) {
					//当前浏览的地图不是下一个要看的
					loadMap(mapCode, name);
					//将上一级地图信息压入mapStack
					mapStack.push({
						mapCode: curMap.mapCode,
						mapName: curMap.mapName
					});
				}
			}

		}, 250);
	});


	function loadSeries(arr) {
		var color = ['#a6c84c', '#633363', '#ef500c','#fab27b'];
		series.splice(0, series.length)
		arr.forEach(function (item, i) {
			if (i != 0) {
				series.push({
					name: item[0] + ' ',
					type: 'lines',
					zlevel: 1,
					effect: {
						show: true,
						period: 6,
						trailLength: 0.7,
						color: '#fff',
						symbolSize: 6
					},
					lineStyle: {
						normal: {
							color: color[i],
							width: 0,
							curveness: 0.2
						}
					},
					data: fconvertData(item[1])
				}, {
					name: item[0] + '',
					type: 'lines',
					zlevel: 3,
					symbol: ['circle', 'circle'],
					symbolSize: 6,
					effect: {
						show: true,
						period: 6,
						trailLength: 0,
						symbol: 'image://./assets/train.png',
						symbolSize: 15
					},
					lineStyle: {
						normal: {
							color: color[i],
							width: 2,
							type:'dashed',
							opacity: 0.6,
							curveness: 0.2
						}
					},
					data: fconvertData(item[1])
				}, {
					name: item[0] + '',
					type: 'scatter',
					coordinateSystem: 'geo',
					zlevel: 2,
					rippleEffect: {
						brushType: 'stroke'
					},
					label: {
						normal: {
							show: true,
							position: 'right',
							formatter: '{b}'
						}
					},
					symbol:'circle',
					symbolSize: function (val) {
						return 6;
					},
					itemStyle: {
						normal: {
							color: color[i]
						}
					},
					data: item[1].map(function (dataItem) {
						return {
							name: dataItem[1].name,
							value: geoCoordMap[dataItem[1].name].concat([dataItem[1].value])
						};
					})
				});

			} else {
				series.push({ // 散点配置
					name: '探测站',
					type: 'effectScatter',
					coordinateSystem: 'geo',
					data: convertData(item),
					symbolSize: 12,
					showEffectOn: 'emphasis',
					rippleEffect: {
						brushType: 'stroke'
					},
					zlevel: 3,
					hoverAnimation: true,
					label: {
						normal: {
							formatter: '{b}',
							position: 'right',
							show: true
						},
						emphasis: {
							show: true
						}
					},
					itemStyle: {
						normal: {
							color: '#ff8003'
						}
					}
				});
			}
		});
	}
	function detail(mapCode,tf,tv,te){
		for(var i=0;i<markPointData[mapCode].length;i++){
			if(markPointData[mapCode][i]['type']=="1"){
				tf.push((markPointData[mapCode][i]));
			}else if(markPointData[mapCode][i]['type']=="2"){
				tv.push((markPointData[mapCode][i]));
			}else if(markPointData[mapCode][i]['type']=="3"){
				te.push((markPointData[mapCode][i]));
			}else if(markPointData[mapCode][i]['type']=="4"){
				// tfault.push((markPointData[mapCode][i]));
			}
			if(markPointData[mapCode][i]['value']=="4"){
				//状态为1 则是故障
				tfault.push((markPointData[mapCode][i]));
			}
		}
		total=total+markPointData[mapCode].length;
	}
	function loadDevice(mapCode){
		 tf=[];
		 tv=[];
		 te=[];
		 tfault=[];
		if('trainmap'==mapCode){
			["haerbinju",
			"shenyangju",
			"beijingju",
			"jinanju",
			"huhehaoteju",
			"taiyuanju",
			"xianju",
			"lanzhouju",
			"zhengzhouju",
			"wuhanju",
			"shanghaiju",
			"nanchangju",
			"guangtiejituan",
			"chengduju",
			"nanningju",
			"kunmingju",
			"qingzanggongsi",
			"mulumuqiju"].forEach(function(item){
				detail(item,tf,tv,te,tfault)
			})
			$("#total").empty().html('设备总数:'+(tv.length+te.length+tf.length)+'台')
		}else{
			for(var i=0;i<markPointData[mapCode].length;i++){
				if(markPointData[mapCode][i]['type']=="1"){
					tf.push((markPointData[mapCode][i]));
				}else if(markPointData[mapCode][i]['type']=="2"){
					tv.push((markPointData[mapCode][i]));
				}else if(markPointData[mapCode][i]['type']=="3"){
					te.push((markPointData[mapCode][i]));
				}else if(markPointData[mapCode][i]['type']=="4"){
					// tfault.push((markPointData[mapCode][i]));
				}
				if(markPointData[mapCode][i]['value']=="4"){
					//状态为1 则是故障
					tfault.push((markPointData[mapCode][i]));
				}
			}
			$("#total").empty().html('设备总数:'+markPointData[mapCode].length+'台')
		}
		$("#tf").empty().html('T F 设备:'+tf.length+'台');
		$("#tv").empty().html('T V 设备:'+tv.length+'台');
		$("#te").empty().html('T E 设备:'+te.length+'台');
		$("#fault").empty().html('故障设备:'+tfault.length+'台');
		/*var interval=setInterval(function(){
			if(document.querySelector("#tf")){
				
			   clearInterval(interval)
			}
		  },1000)*/
	}
	function loadMap(mapCode, mapName) {
		var option = {}; // echarts 配置
		var temparr = [];
		loadDevice(mapCode);
		temparr.splice(0, temparr.length);
		currentMapcode = mapCode;
		lastPoints = markPointData[mapCode] ? markPointData[mapCode] : lastPoints
		//添加标记点
		temparr.push(lastPoints);
		//添加铁路线路
		temparr=temparr.concat(flyLinesData[mapCode] ? flyLinesData[mapCode] : [['',[]]]);
		// temparr.push(['上海', SHData]);
		loadSeries(temparr)
		$.get('./json/' + mapCode + '.json?r='+Math.random(), function (data) {
			if (data) {
				echarts.registerMap(mapCode, data);
				option = { // echarts 配置
					backgroundColor: '#fff',
					title: {
						text: '科佳设备分布图',
						subtext: '2019年7月18日',
						// sublink: 'http://www.pm25.in',
						left: 'center',
						textStyle: {
							color: '#666666'
						}
					},
					tooltip: {
						trigger: 'item',
						formatter: function (params) {
							if (params.seriesType == "effectScatter") {
								var type=""
								//1tf 2tv 3te 
								if(params.data.type=='1'){
									type="TF"
								}else if(params.data.type=='2'){
									type="TV"
								}else if(params.data.type=='3'){
									type="TE"
								}
								if(params.data.value[2]!='4'){
									return '<p>探测站：' + params.data.name + '</p><p>设备类型：'+type+'</p><p>设备状态:正常</p></p>'
								}else{
									return '<p>探测站：' + params.data.name + '</p><p>设备类型：'+type+'</p><p>设备状态:故障</p></p>'
								}
								
								//return "线路："+params.data.name+""+params.data.value[2];
							} else if (params.seriesType == "lines") {
								return  params.seriesName+"<br />" +params.data.fromName + "至" + params.data.toName;
							} else {
								return params.name;
							}

							//return '<a target="_blank" class="params-name" href="detail.html?label='+params.data.name+'">' + params.data.name + '</a><p class="conut-next">管理干部培训&nbsp;&nbsp;' + params.data.runConut + '人</p><p class="conut-next">卓越教师培训&nbsp;&nbsp;' + params.data.num + '人</p>'
						},
						enterable: true, //鼠标是否可进入提示框
						transitionDuration: 3, //提示框移动动画过渡时间
						backgroundColor: '#fffeee',
						borderWidth: '1px',
						borderRadius: '5',
						borderColor: 'rgba(72,150,128,1)',
						textStyle: {
							color: 'rgba(94,194,222,1)'
						},
						padding: 10,
						showDelay: 0,
						transitionDuration: 0
					},

					visualMap: {
						min: 1,
						max: 4,
						calculable: true,
						inRange: {
							color: ['#005831', '#11264f', '#f47920','#ff0000'],
							symbol:['path://M586.67008 1005.50656h-512v-53.20704c96.01024 3.56352 140.41088-26.624 133.34528-90.46016V212.66432c7.08608-63.85664-37.33504-93.94176-133.34528-90.46016V68.99712h842.67008l10.67008 335.21664h-58.6752c-49.80736-180.89984-156.48768-271.36-319.97952-271.36-81.83808-7.0656-119.17312 21.2992-112.00512 85.13536v266.05568c117.32992 10.6496 188.416-42.57792 213.34016-159.62112h53.32992v377.79456h-53.32992c-24.92416-117.06368-96.01024-170.27072-213.34016-159.62112v319.26272c-7.18848 63.83616 42.65984 94.02368 149.31968 90.46016v53.18656z',
							 'path://M182.5 155c-5.8-13.1-14.4-22.6-34.6-31L52.8 84.8C41.3 80 35.6 75.2 35.6 70.5c0-3.6 8.6-4.8 25.9-4.8h446.6c8.6 0 17.3 1.2 17.3 4.8 0 4.8-5.8 9.5-14.4 13.1l-74.9 28.6c-23.1 8.3-37.5 21.4-34.6 32.2l147 418.1h5.8l121-418.1c2.9-10.7-11.5-22.6-34.6-32.2l-69.2-28.6c-8.6-3.6-17.3-8.3-17.3-13.1 0-3.6 11.5-4.8 20.2-4.8h386.1c17.3 0 25.9 1.2 25.9 4.8 0 4.8-5.8 9.5-17.3 14.3L874 124.1c-20.2 8.3-31.7 17.9-34.6 31L611.8 946c-2.9 8.3-11.5 13.1-28.8 13.1H482.2c-14.4 0-20.2-3.6-23.1-9.5L182.5 155z', 
							 'path://M954.12224 0l11.6736 366.65344h-64.02048C847.6672 168.81664 731.21792 69.75488 552.73472 69.75488c-89.12896-7.68-129.8432 23.32672-122.14272 93.10208v290.89792c128.1024 11.65312 205.55776-46.3872 232.59136-174.4896h58.34752v413.06112h-58.34752c-27.0336-128.1024-104.48896-186.20416-232.59136-174.53056v343.32672c-3.95264 65.78176 36.7616 96.82944 122.14272 93.10208 193.8432-3.97312 317.99296-110.71488 372.36736-320h64.02048L977.44896 1024H34.87744v-58.10176c104.73472 3.72736 153.16992-29.2864 145.46944-99.04128V157.14304c7.70048-69.77536-40.71424-102.76864-145.46944-99.04128V0h919.2448z',
							 'path://M512 0C229.376 0 0 229.376 0 512s229.376 512 512 512 512-229.376 512-512S794.624 0 512 0z m0 819.2c-29.184 0-52.224-23.552-52.224-52.224 0-29.184 23.552-52.224 52.224-52.224 29.184 0 52.224 23.552 52.224 52.224S541.184 819.2 512 819.2z m56.832-584.192l-10.752 372.736c-0.512 19.456-20.48 34.816-44.032 34.816h-8.192c-23.552 0-43.52-15.36-44.032-34.816L450.56 235.008c-1.024-29.696 23.04-54.784 53.76-54.784h10.752c30.72 0 54.784 25.088 53.76 54.784z'],
							// symbol:['circle', 'image://./assets/location.png', 'pin', 'triangle'],
							//'path://M143.6 664.2l43.4-51c33.8 33.8 79.7 55.5 125 55.5 56.8 0 88.6-26.8 88.6-66.3 0-42.7-31.9-56.1-75.2-75.2l-65-28.1c-45.3-19.1-95.6-54.8-95.6-123.1 0-74 65-129.4 154.3-129.4 54.8 0 106.5 23 142.2 59.3L422.2 353c-29.3-26.1-61.9-42.1-103.3-42.1-47.8 0-79.1 23-79.1 60.6 0 40.2 37.6 55.5 75.9 70.8l64.4 27.4c55.5 23.6 95.6 58 95.6 126.9 0 75.2-62.5 136.5-165.2 136.5-64.3 0-124.2-25.6-166.9-68.9zM673.5 546.9L532.6 255.4h79.1l54.2 124.3c14.7 35.1 28.1 67.6 44 103.3h2.6c15.3-35.7 30.6-68.2 44.6-103.3l54.8-124.3H889L747.4 546.9v177.9h-74V546.9z',
							symbolSize: 12
						},
						textStyle: {
							color: '#fff'
						},
						seriesIndex:0, 
						show: false,
					},
					geo: { // 地图配置
						// type: 'map',
						//地图名称
						map: mapCode,
						label: {
							normal: {
								formatter: function (params) {
									var res = "";
									if (params.name) {
										//修改此处代码弃用stationInfo 统计 因为设计还没定现在不用
										//res = params.name + (cityMap[params.name] ? '\n设备总数：' + markPointData[cityMap[params.name]].length : '') + '\n{lc|}'
										res = params.name + (stationInfo[params.name] ? '\n设备总数：' + stationInfo[params.name].count : '') + '\n{lc|}'
									} else {

									}
									return res;
								},
								position: 'inside',
								show: true,
								rich: {
									lc: {
										backgroundColor: {
											image: './assets/location.png'
										}
									},
									la: {
										fontsize: '8px',
										color: '#ffffff',
									}
								}
							},
							emphasis: {
								show: false
							},
						},
						roam: true, //谷歌有兼容问题 在火狐好使
						itemStyle: {
							normal: {
								areaColor: '#fff',
								borderColor: '#3B5077'
							},
							emphasis: {
								areaColor: '#ffffff'
							}
						},
						zoom: 1.2,
						// regions:[{                                  //在地图中对特定的区域配置样式。
						// 	name: '哈尔滨局',
						// 	itemStyle: {
						// 		normal: {
						// 			areaColor: '#ffddee',
						// 			color: 'red'
						// 		}
						// 	}
						// }]

					},
					series: series
					//[
					// 	{
					// 		name:  ' Top10',
					// 		type: 'lines',
					// 		zlevel: 1,
					// 		effect: {
					// 			show: true,
					// 			period: 6,
					// 			trailLength: 0.7,
					// 			color: '#fff666',
					// 			symbolSize: 3
					// 		},
					// 		lineStyle: {
					// 			normal: {
					// 				color: color[0],
					// 				width: 0,
					// 				curveness: 0.2
					// 			}
					// 		},
					// 		data: fconvertData(flyLinesData[mapCode]?flyLinesData[mapCode]:[])
					// 	},
					// 	{
					// 		name: ' Top10',
					// 		type: 'lines',
					// 		zlevel: 2,
					// 		symbol: ['none', 'circle'],
					// 		symbolSize: 10,
					// 		effect: {
					// 			show: true,
					// 			period: 6,
					// 			trailLength: 0,
					// 			symbol: 'image://./assets/train.png',
					// 			symbolSize: 22
					// 		},
					// 		lineStyle: {
					// 			normal: {
					// 				color: color[0],
					// 				width: 2,
					// 				opacity: 0.6,
					// 				curveness: 0.2
					// 			}
					// 		},
					// 		data: fconvertData(flyLinesData[mapCode]?flyLinesData[mapCode]:[])
					// 	},
					// 	{ // 散点配置
					// 	name: '探测站',
					// 	type: 'effectScatter',
					// 	coordinateSystem: 'geo',
					// 	data: convertData(lastPoints),
					// 	symbolSize: 12,
					// 	showEffectOn: 'emphasis',
					// 	rippleEffect: {
					// 		brushType: 'stroke'
					// 	},
					// 	zlevel: 2,
					// 	hoverAnimation: true,
					// 	label: {
					// 		normal: {
					// 			formatter: '{b}',
					// 			position: 'right',
					// 			show: true
					// 		},
					// 		emphasis: {
					// 			show: true
					// 		}
					// 	},
					// 	itemStyle: {
					// 		normal: {
					// 			color: '#ff8003'
					// 		}
					// 	}
					// }]
				};
				myCharts.clear();
				// console.log(mapName);
				// console.log(option);
				option.title.text=mapName+"设备分布图";
				myCharts.setOption(option);
				curMap = {
					mapCode: mapCode,
					mapName: mapName
				};
			}
		})
	}


});